'''
Created on May 25, 2016

@author: JimmyMo
'''
from dispatchtarget_rabbitmq import DispatchTarget_RabbitMQ
from dispatchmethod_timer import DispatchMethod_Timer
import threading
import time

class MessageBuilderCollector(object):
    def __init__(self, config_reader):
        self.config_reader = config_reader
    
    def build(self):
        msg = time.strftime('%Y%m%d%H%M%S', time.localtime());
        return "hello, task id:%s" % msg;
        
class DispatcherCollector(threading.Thread):
    def __init__(self, config_reader):
        super(DispatcherCollector, self).__init__()
        self.config_reader = config_reader
        self.name = "dispatcher_collector"
    def run(self):
        self.dispatch()
        
    def dispatch(self):
        interval = int(self.config_reader.items["Collector"]["Interval"])
        queue_name = self.config_reader.items["Collector"]["QueueName"]
        routing_key = self.config_reader.items["Collector"]["RoutingKey"]
        rabbitmq_host = self.config_reader.items["General"]["RabbitMQHost"]
        
        target = DispatchTarget_RabbitMQ(host=rabbitmq_host, queue_name=queue_name, routing_key=routing_key, message_builder=MessageBuilderCollector(self.config_reader))
        
        
#         target.construct_message("hello, task id:%s" % msg)
        method = DispatchMethod_Timer(interval)
        
        method.set_method_target(target)
        method.dispatch()